import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  ParseUUIDPipe,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { Permissions, PermissionsGuard } from '@nbase/nest-mods';
import type { IMenu, IMenuTree, IPaginationResponse } from '@nbase/shared';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { QueryMenuDto } from './dto/query-menu.dto';

/**
 * 菜单控制器
 */
@ApiTags('system/menu')
@ApiBearerAuth('JWT-auth')
@Controller('system/menu')
@UseGuards(PermissionsGuard)
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  /**
   * 查询所有菜单（分页查询）
   */
  @ApiOperation({ summary: '获取菜单列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:menu:query')
  @Get()
  findAll(@Query() query: QueryMenuDto): Promise<IPaginationResponse<IMenu>> {
    return this.menuService.paginateMenus(query);
  }

  /**
   * 获取菜单树
   */
  @ApiOperation({ summary: '获取菜单树' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:menu:query')
  @Get('tree')
  getTree(): Promise<IMenuTree[]> {
    return this.menuService.getMenuTree();
  }

  /**
   * 根据 ID 查询菜单
   */
  @ApiOperation({ summary: '获取菜单详情' })
  @ApiParam({ name: 'id', description: '菜单ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '菜单不存在' })
  @Permissions('system:menu:query')
  @Get(':id')
  findOne(@Param('id', ParseUUIDPipe) id: string): Promise<IMenu> {
    return this.menuService.findById(id);
  }

  /**
   * 获取子菜单
   */
  @ApiOperation({ summary: '获取子菜单列表' })
  @ApiParam({ name: 'id', description: '父菜单ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:menu:query')
  @Get(':id/children')
  getChildren(@Param('id', ParseUUIDPipe) id: string): Promise<IMenu[]> {
    return this.menuService.getChildMenus(id);
  }

  /**
   * 创建菜单
   */
  @ApiOperation({ summary: '创建菜单' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @Permissions('system:menu:create')
  @Post()
  create(@Body() createMenuDto: CreateMenuDto): Promise<IMenu> {
    return this.menuService.createMenu(createMenuDto);
  }

  /**
   * 更新菜单
   */
  @ApiOperation({ summary: '更新菜单' })
  @ApiParam({ name: 'id', description: '菜单ID', type: String })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 404, description: '菜单不存在' })
  @Permissions('system:menu:update')
  @Put(':id')
  update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() updateMenuDto: UpdateMenuDto
  ): Promise<IMenu> {
    return this.menuService.updateMenu(id, updateMenuDto);
  }

  /**
   * 删除菜单
   */
  @ApiOperation({ summary: '删除菜单' })
  @ApiParam({ name: 'id', description: '菜单ID', type: String })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '菜单不存在' })
  @Permissions('system:menu:delete')
  @Delete(':id')
  async remove(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
    await this.menuService.deleteMenu(id);
  }
}

